package com.example.wanbaqian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.FrameLayout;

import com.example.wanbaqian.R;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.event.ChangeEvent;
import com.example.wanbaqian.fragment.AddMoneyFragment;
import com.example.wanbaqian.fragment.MyFragment;
import com.example.wanbaqian.fragment.WalletFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 主界面
 */
public class HomeActivity extends BaseActivity {
    @BindView(R.id.home_FrameLayout)
    FrameLayout homeFrameLayout;
    @BindView(R.id.home_TabLayout)
    CommonTabLayout homeTabLayout;
    private String[] mTitles = {"借", "还", "我"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private int[] mIconUnselectIds = {
            R.mipmap.ic_jie,R.mipmap.ic_huan,R.mipmap.ic_me
    };
    private int[] mIconSelectIds = {
            R.mipmap.ic_jie_checked,R.mipmap.ic_huan_checked,R.mipmap.ic_me_checked};

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private static final String TAG = "HomeActivity";
    private HomeReceiver homeReceiver;
    private FragmentManager mFm;
    private FragmentTransaction mTransaction;
    private int currentFragmentPosition = 0;

    @Override

    protected int getLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        Log.e("LOGIN_SU", "主页初始化时间: "+System.currentTimeMillis());
        setStatusBarColorInFragment();
        mFragments.add(WalletFragment.newInstance(""));
        mFragments.add(AddMoneyFragment.newInstance(""));
        mFragments.add(MyFragment.newInstance(""));
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabLayoutAdapter(i));
        }
        homeTabLayout.setTabData(mTabEntities, this, R.id.home_FrameLayout, mFragments);
        regReceiver();
        //当上次保存的界面存在时就显示上次的界面
        if (null != savedInstanceState) {
            homeTabLayout.setCurrentTab(savedInstanceState.getInt("CurrentTab", 0));
        }
    }
    /**
     * 解决部分手机内存不够回收了activity的时候fragment出现重叠的问题
     * 当FragmentManager使用hide和show切换fragment时会出现布局错乱
     * 保存当前的界面
     *
     * @param outState
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
        outState.putInt("CurrentTab", homeTabLayout.getCurrentTab());
    }

    @Override
    protected void initDate() {

    }
    /**
     * 首页布局适配器
     */
    class TabLayoutAdapter implements CustomTabEntity {
        int position = 0;

        public TabLayoutAdapter(int position) {
            this.position = position;
        }

        @Override
        public String getTabTitle() {
            return mTitles[position];
        }

        @Override
        public int getTabSelectedIcon() {
            return mIconSelectIds[position];
        }

        @Override
        public int getTabUnselectedIcon() {
            return mIconUnselectIds[position];
        }
    }
    /**
     * 首页广播
     */
    class HomeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(CommonValue.HomeReceiver)) {
                try {
                    homeTabLayout.setCurrentTab(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangeEvent(ChangeEvent event){
        homeTabLayout.setCurrentTab(0);
    }

    /**
     * Fragment 跳转
     * @param position
     */
    public void changeFragment(final int position) {
        mFm = getSupportFragmentManager();
        mTransaction = mFm.beginTransaction();
        if (position != currentFragmentPosition) {
            mTransaction.hide(mFragments.get(position));
            mTransaction.hide(mFragments.get(position));
            if (!mFragments.get(position).isAdded()) {
                mTransaction.add(R.id.home_FrameLayout, mFragments.get(position));
            }
            mTransaction.show(mFragments.get(position));
            mTransaction.commit();
        }
        currentFragmentPosition = position;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(homeReceiver);
    }

    /**
     * 退出程序，点击返回键时
     */
    @Override
    public void onBackPressed() {
        finish();
    }

    /**
     * 注册广播
     */
    private void regReceiver() {
        homeReceiver = new HomeReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CommonValue.HomeReceiver);
        registerReceiver(homeReceiver, intentFilter);
    }
}
